//
//  PMAPI.m
//  Text_ problem
//
//  Created by jiboxue on 13-1-23.
//  Copyright (c) 2013年 jiboxue. All rights reserved.
//

#import "PMAPI.h"
#import "ASIHTTPRequest.h"
#import "ASIFormDataRequest.h"
#import "json.h"

@implementation PMAPI
+(ASIHTTPRequest *)requestLoginUsername:(NSString *)username andPassword:(NSString *)password
{
    NSString *url_string = [NSString stringWithFormat:@"%@/account/login",pm_api];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url_string]];
    [request addBasicAuthenticationHeaderWithUsername:username andPassword:password];
    return request;
}
+(ASIFormDataRequest *)requestModifyName:(NSString *)ModifyName andUsername:(NSString *)username andPassword:(NSString *)password
{
    NSString *url_string = [NSString stringWithFormat:@"%@/account/update_profile_nickname",pm_api];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url_string]];
    [request setPostValue:ModifyName forKey:@"nickname"];
    [request addBasicAuthenticationHeaderWithUsername:username andPassword:password];
    return request;
}
+(ASIFormDataRequest *)requestHeadImage:(NSString *)headimage andUsername:(NSString *)username andPassword:(NSString *)password
{
    NSString *url_string = [NSString stringWithFormat:@"%@/account/update_profile_image",pm_api];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url_string]];
    [request addFile:headimage
        withFileName:@"avatar.jpeg"
      andContentType:@"image/jpeg"
              forKey:@"image"];
    [request addBasicAuthenticationHeaderWithUsername:username andPassword:password];
    return request;
}
+(ASIFormDataRequest *)requestChangeNewPassword:(NSString *)NewPassword andOldPassword:(NSString *)OldPassword andUserID:(NSString *)UserID
{
    NSString *url_string = [NSString stringWithFormat:@"%@/account/changepassword",pm_api];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url_string]];
    [request setPostValue:UserID forKey:@"user_id"];
    [request setPostValue:OldPassword forKey:@"old_password"];
    [request setPostValue:NewPassword forKey:@"new_password"];
    return request;
}

+(ASIHTTPRequest *)requestlatestUsernsme:(NSString *)username andPassword:(NSString *)password
{
    NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
    NSString *url_string = nil;
    if ([bundleIdentifier isEqualToString:@"com.td.pm2"]) {
        url_string = [NSString stringWithFormat:@"%@/pm_question/latest",pm_api];
    }
    else
    {
        url_string = [NSString stringWithFormat:@"%@/office_question/latest",pm_api];
    }
    
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:url_string]];
    [request addBasicAuthenticationHeaderWithUsername:username andPassword:password];
    return request;
}

+ (ASIFormDataRequest *)requestUsername:(NSString *)username
                            andPassword:(NSString *)password
                            andQuestion:(NSDictionary *)question1
                            andQuestion:(NSDictionary *)question2
                            andAnswer_1:(NSString *)answer_1
                            andAnswer_2:(NSString *)answer_2
{
    NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
    NSString *url_string = nil;
    if ([bundleIdentifier isEqualToString:@"com.td.pm2"]) {
        url_string = [NSString stringWithFormat:@"%@/pm_question/answer",pm_api];
    }
    else
    {
        url_string = [NSString stringWithFormat:@"%@/office_question/answer",pm_api];
    }
    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url_string]];
    [request addBasicAuthenticationHeaderWithUsername:username
                                          andPassword:password];
    
    NSString *questionID = [question1 objectForKey:@"id"];
    NSString *question_goup_id = [question1 objectForKey:@"question_goup_id"];
    NSString *begin_time = [question1 objectForKey:@"begin_time"];
    NSString *content_userID = [question1 objectForKey:@"content_userID"];
    //self.questionID =
    NSString *questionID1 = [question2 objectForKey:@"id"];
    NSString *begin_time1 = [question2 objectForKey:@"begin_time"];
    NSString *content_userID1 = [question2 objectForKey:@"content_userID"];
    
    [request setPostValue:question_goup_id forKey:@"question_group_id"];
    [request setPostValue:questionID forKey:@"questionID_1"];
    [request setPostValue:questionID1 forKey:@"questionID_2"];
    [request setPostValue:begin_time forKey:@"answer_beginTime1"];
    [request setPostValue:begin_time1 forKey:@"answer_beginTime2"];
    [request setPostValue:content_userID forKey:@"content_userID1"];
    [request setPostValue:content_userID1 forKey:@"content_userID2"];
    
    [request setPostValue:answer_1 forKey:@"answer_1"];
    [request setPostValue:answer_2 forKey:@"answer_2"];
    
    return request;
    
//    return request;
}
+ (ASIFormDataRequest *)requestUsername:(NSString *)username
                            andPassword:(NSString *)password
                            andQuestion:(NSDictionary *)question1
                            andQuestion:(NSDictionary *)question2
{
    NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
    NSString *url_string = nil;
    if ([bundleIdentifier isEqualToString:@"com.td.pm2"]) {
        url_string = [NSString stringWithFormat:@"%@/pm_question/valid",pm_api];
    }
    else
    {
        url_string = [NSString stringWithFormat:@"%@/office_question/valid",pm_api];
    }
    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url_string]];
    [request addBasicAuthenticationHeaderWithUsername:username
                                          andPassword:password];
    NSString *questionID_1 = [question1 objectForKey:@"id"];
    NSString *questionID_2 = [question2 objectForKey:@"id"];
    NSString *begin_time = [question1 objectForKey:@"begin_time"];
    NSString *begin_time1 = [question2 objectForKey:@"begin_time"];
    NSString *contentUserID1 = [question1 objectForKey:@"content_userID"];
    NSString *contentUserID2 = [question2 objectForKey:@"content_userID"];
    NSString *groupID = [question1 objectForKey:@"question_goup_id"];
    
    [request setPostValue:questionID_1 forKey:@"questionID_1"];
    [request setPostValue:questionID_2 forKey:@"questionID_2"];
    [request setPostValue:begin_time forKey:@"answer_beginTime1"];
    [request setPostValue:begin_time1 forKey:@"answer_beginTime2"];
    [request setPostValue:contentUserID1 forKey:@"content_userID1"];
    [request setPostValue:contentUserID2 forKey:@"content_userID2"];
    [request setPostValue:groupID forKey:@"question_group_id"];
    
    return request;
}

+ (ASIHTTPRequest *)registerNotification:(NSString *)deviceToken
                             andUsername:(NSString *)username
                             andPassword:(NSString *)password
{
    
    NSString *url = [NSString stringWithFormat:@"%@/notification/register",pm_api];
    
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:url]];
    
    [request addBasicAuthenticationHeaderWithUsername:username
                                          andPassword:password];
    
    [request setPostValue:deviceToken forKey:@"device_token"];
    [request setPostValue:@"pm_office" forKey:@"app_name"];
    
    return request;
}

@end
